from bs4 import BeautifulSoup
import requests
import csv
from time import sleep

# 设置请求头模拟浏览器访问
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

def scrape_month_data(year, month):
    """爬取指定年月的天气数据"""
    # 格式化月份为两位数
    month_str = f"{month:02d}"
    url = f'https://www.tianqihoubao.com/lishi/dalian/month/{year}{month_str}.html'

    try:
        response = requests.get(url, headers=headers)
        response.encoding = 'utf-8'
        soup = BeautifulSoup(response.text, 'lxml')

        # 定位天气数据表格
        table = soup.find('table', {'class': 'weather-table'})
        if not table:
            print(f"未找到表格数据: {year}年{month}月")
            return []

        monthly_temps = []
        # 提取表格数据
        for row in table.find_all('tr')[1:]:  # 跳过表头行
            cols = row.find_all('td')
            if len(cols) >= 4:  # 确保有完整的数据列
                temp = cols[2].get_text(strip=True).split('/')
                temp_high = temp[0].replace('℃', '').strip()
                if temp_high:  # 确保温度值不为空
                    try:
                        monthly_temps.append(float(temp_high))
                    except ValueError:
                        continue

        if monthly_temps:
            avg_temp = round(sum(monthly_temps) / len(monthly_temps), 2)
            print(f"成功爬取 {year}年{month}月 数据，平均最高气温: {avg_temp:.2f}℃")
            return (f"{year}-{month_str}", avg_temp)
        else:
            print(f"{year}年{month}月 无有效温度数据")
            return None

    except Exception as e:
        print(f"爬取 {year}年{month}月 数据时出错: {e}")
        return None


def main():
    # 准备写入CSV文件
    with open('monthly_avg_temp_2025_real.csv', 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.writer(csvfile)
        # 写入表头
        writer.writerow(['日期(年月)', '平均最高气温(℃)'])

        # 爬取2022-2024年每月数据
        year = 2025
        for month in range(1, 7):
            result = scrape_month_data(year, month)
            if result:
                writer.writerow(result)
            sleep(0.5)  # 防止请求过于频繁

    print("所有数据已成功保存到 monthly_avg_temp_2025_real.csv")


if __name__ == '__main__':
    main()